import { SiteStore } from '@/stores/site'

const utilFuns = {
  utc2beijing(utc_datetime) {
    if (!utc_datetime) {
      return ''
    }
    // 转为正常的时间格式 年-月-日 时:分:秒
    var T_pos = utc_datetime.indexOf('T')
    var Z_pos = utc_datetime.indexOf('Z')
    var year_month_day = utc_datetime.substr(0, T_pos)
    var hour_minute_second = utc_datetime.substr(T_pos + 1, Z_pos - T_pos - 1)
    var new_datetime = year_month_day + ' ' + hour_minute_second // 2017-03-31 08:02:06

    // 处理成为时间戳
    timestamp = new Date(Date.parse(new_datetime))
    timestamp = timestamp.getTime()
    timestamp = timestamp / 1000

    // 增加8个小时，北京时间比utc时间多八个时区
    var timestamp = timestamp + 8 * 60 * 60

    // 时间戳转为时间
    var beijing_datetime = new Date(parseInt(timestamp) * 1000)
      .toLocaleString()
      .replace(/年|月/g, '-')
      .replace(/日/g, ' ')
    return beijing_datetime // 2017-03-31 16:02:06
  },

  copy: function (obj) {
    return JSON.parse(JSON.stringify(obj))
  },
  timestamp: function () {

    var t = new Date();


    return t.getTime();

  },

  timeident: function () {

    const timestamp = this.timestamp();


    return timestamp.toString(36);


  },


  parseTime(time, cFormat) {
    if (arguments.length === 0 || !time) {
      return null
    }
    const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
    let date
    if (typeof time === 'object') {
      date = time
    } else {
      if ((typeof time === 'string')) {
        if ((/^[0-9]+$/.test(time))) {
          // support "1548221490638"
          time = parseInt(time)
        } else {
          // support safari
          // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
          time = time.replace(new RegExp(/-/gm), '/')
        }
      }

      if ((typeof time === 'number') && (time.toString().length === 10)) {
        time = time * 1000
      }
      date = new Date(time)
    }
    const formatObj = {
      y: date.getFullYear(),
      m: date.getMonth() + 1,
      d: date.getDate(),
      h: date.getHours(),
      i: date.getMinutes(),
      s: date.getSeconds(),
      a: date.getDay()
    }
    const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
      const value = formatObj[key]
      // Note: getDay() returns 0 on Sunday
      if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
      return value.toString().padStart(2, '0')
    })
    return time_str
  },


  fullimg: function (url) {
    const Site = SiteStore()

    var rds = Site.rds

    if (!url) {
      return url
    }
    if (url.indexOf('http') === 0 || url.indexOf('//') === 0) {
      return url
    } else {
      return rds + url
    }
  },
  fulllocal: function (url, data = null) {
    const Site = SiteStore()

    var baseurl = Site.baseurl


    if (data) {

      var arrstrpara = []

      for (var key in data) {

        arrstrpara.push(`${key}=${data[key]}`)
      }
      if (url.indexOf('?') > -1) {

        url = url + '&'
      }
      else {
        url = url + '?'
      }

      url += arrstrpara.join('&')
    }



    if (!url) {
      return url
    }
    if (url.indexOf('http') === 0 || url.indexOf('//') === 0) {
      return url
    } else {
      return baseurl + "/" + url
    }
  },

  randomPassword(size) {
    var seed = new Array(
      'A',
      'B',
      'C',
      'D',
      'E',
      'F',
      'G',
      'H',
      'I',
      'J',
      'K',
      'L',
      'M',
      'N',
      'P',
      'Q',
      'R',
      'S',
      'T',
      'U',
      'V',
      'W',
      'X',
      'Y',
      'Z',
      'a',
      'b',
      'c',
      'd',
      'e',
      'f',
      'g',
      'h',
      'i',
      'j',
      'k',
      'm',
      'n',
      'p',
      'Q',
      'r',
      's',
      't',
      'u',
      'v',
      'w',
      'x',
      'y',
      'z',
      '2',
      '3',
      '4',
      '5',
      '6',
      '7',
      '8',
      '9'
    ) //数组
    var seedlength = seed.length //数组长度
    var createPassword = ''
    var j = 0
    for (var i = 0; i < size; i++) {
      j = Math.floor(Math.random() * seedlength)
      createPassword += seed[j]
    }
    return createPassword
  }
}

const files = import.meta.globEager('./tool/*.js')
Object.keys(files).forEach((fileName) => {
  const name = fileName.replace(/\.\/tool\/|\.js/g, '')
  utilFuns[name] = files[fileName].default
})

export default utilFuns
